﻿package component.fileupload.action;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import component.fileupload.listener.MonitoredDiskFileItemFactory;
import component.fileupload.listener.UploadFileListener;
import component.fileupload.util.FileUtil;
import component.fileupload.vo.UploadFileVO;
import component.fileupload.exception.FileUploadException;

public class ServletFileUploadAction extends AbstractFileUploadAction {
	public InputStream[] getInputStreams(ServletConfig config, HttpServletRequest request, HttpServletResponse response) throws FileUploadException{
		HttpSession session = request.getSession();
		response.setContentType("text/html;charset=" + FileUtil.CHARSET);
		
		fileInfoMap = new HashMap();
		try {
			// 创建UploadListener对象
			listener = new UploadFileListener(request.getContentLength());
			// 启动监听状态
			listener.start();
			// 将监听器对象的状态保存在Session中
			session.setAttribute("FILE_UPLOAD_STATS", listener.getState());
			// 创建MonitoredDiskFileItemFactory对象
			FileItemFactory factory = new MonitoredDiskFileItemFactory(listener);
			
			// 通过该工厂对象创建ServletFileUpload对象
			ServletFileUpload upload = new ServletFileUpload(factory);
			upload.setSizeMax(FileUtil.DEFAULT_SIZE_MAX);//设置上传文件的总大小

			// 将转化请求保存到list对象中
			List items = upload.parseRequest(request);

			// 循环list中的对象
			for (Iterator i = items.iterator(); i.hasNext();) {
				FileItem fileItem = (FileItem) i.next();
				if (!fileItem.isFormField() && !"".equals(fileItem.getName())) {// 如果该FileItem不是表单域,并且上传文件不为空
					UploadFileVO vo = new UploadFileVO();
					String filePath = fileItem.getName();
					String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
					
					InputStream in = fileItem.getInputStream();
					vo.setInputStream(in);
					//vo.setIndex(fileInfoMap.size());
					vo.setIndex(Integer.parseInt(fileItem.getFieldName().split("_")[1]));
					System.out.println("文件的顺序号：" + vo.getIndex());
					vo.setFieldName(fileItem.getFieldName());
					vo.setFilePath(filePath);
					vo.setFileName(fileName);
					vo.setFileSize(fileItem.getSize());
					vo.setDestroyObj(fileItem);//在最后才删除该对象
					fileInfoMap.put(new Integer(fileInfoMap.size()), vo);
					
					//fileItem.delete();// 内存中删除该数据流(在销毁方法里执行)
				}
			}
			
		} catch (org.apache.commons.fileupload.FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			listener.error(e.getMessage());
			throw new FileUploadException(e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			listener.error(e.getMessage());
			throw new FileUploadException(e);
		}
		
		InputStream[] inArr = new InputStream[fileInfoMap.size()];
		for(int i = 0;i<fileInfoMap.size();i++){
			UploadFileVO vo = (UploadFileVO)fileInfoMap.get(new Integer(i));
			inArr[i] = vo.getInputStream();
		}
		return inArr;

	}
	
	public void destroy(){
		this.listener = null;
		
		//先销毁里面的数据
		for(int i = 0;i<fileInfoMap.size();i++){
			UploadFileVO vo = (UploadFileVO)fileInfoMap.get(new Integer(i));
			InputStream in = vo.getInputStream();
			try {
				if(in!=null) in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			((FileItem)vo.getDestroyObj()).delete();
		}
		fileInfoMap = null;
	}
	

}
